package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author stone
 * @date 2022/09/06 11:41
 */
@RestController
@RequestMapping("admin")
public class AdminAdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("admin/query/{id}")
    public BaseRespVo query(@PathVariable("id") Integer id) {
        MarketAdmin marketAdmin = adminService.queryById(id);
        return BaseRespVo.ok(marketAdmin);
    }

    @RequestMapping("admin/update")
    public BaseRespVo update(@RequestBody MarketAdmin admin) {
        admin.setUpdateTime(new Date());
        adminService.modify(admin);
        return BaseRespVo.ok(null);
    }

    // where username = ? and password like ? and avatar in (?,?,?)
    @RequestMapping("admin/list")
    public BaseRespVo list(MarketAdmin marketAdmin, BasePageInfo info) {
        CommonData<MarketAdmin> data = adminService.queryAdmin(marketAdmin,info);
        return BaseRespVo.ok(data);
    }


/*    @RequestMapping("role/options")
    public BaseRespVo roleOptions(){
        CommonData<MarketRole> data = adminService.queryRole();
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }*/

    @RequestMapping("admin/create")
    public BaseRespVo adminCreate(@RequestBody MarketAdmin marketAdmin){
        Date date = new Date();
        marketAdmin.setAddTime(date);
        marketAdmin.setUpdateTime(date);
        marketAdmin.setLastLoginTime(date);
        MarketAdmin data = adminService.createAdmin(marketAdmin);
        BaseRespVo ok = BaseRespVo.ok(data);
        ok.setErrmsg("成功");
        return ok;
    }

    @RequestMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody MarketAdmin marketAdmin){
        adminService.deleteAdmin(marketAdmin);
        BaseRespVo ok = BaseRespVo.ok(null);
        ok.setErrmsg("成功");
        return ok;
    }
}
